
package com.zhuzhe.blog.convert;

import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @description  转换请求及返回
 * @author: u53
 * @create: 2020-10-15 11:26
 **/
public class Convert {

    public static <T> void convertType(T in , T out){
        //判断传入值是否为空 空则结束
        if (null == in){
            return;
        }
        BeanUtils.copyProperties(in, out);
    }



    public static <T> List<T> convertList(List<?> oldList, Class<T> clazz){
        //判断被复制的list是否为空
        if (null == oldList ){
            return new ArrayList<>( 0 );
        }
        //初始化新list
        List<T> newList = new ArrayList<>();
        oldList.forEach( o -> {
            try {
                T o1 = clazz.newInstance();
                BeanUtils.copyProperties( o,o1 );
                newList.add( o1 );
            } catch (InstantiationException | IllegalAccessException e) {
                e.printStackTrace();
            }
        } );
        return newList;
    }


}
